<template>
  <mall-sale></mall-sale>
  <div class="mall-content" v-loading="loading">
    <div class="mall-item" v-for="item in mallList" :key="item.name">
      <mall-cart
        :data="item"
        @SetOff="handleSetOff"
        @setMall="setMall"
      ></mall-cart>
    </div>
  </div>
  <div class="set-mall">
    <el-dialog v-model="setMallFormData" title="修改商品信息" width="40%">
      <el-form :model="form" inline>
        <el-form-item label="商品图片">
          <div class="set-mall-img">
            <img @click="handleImg" :src="form.src" />
          </div>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input v-model.trim="form.name" placeholder="商品名称" />
        </el-form-item>
        <el-form-item label="商品价格">
          <el-input v-model.trim="form.price" placeholder="商品价格" />
        </el-form-item>
        <el-form-item label="商品折扣价">
          <el-input v-model.trim="form.discount" placeholder="商品折扣价" />
        </el-form-item>
        <el-form-item label="是否下架">
          <el-select v-model.trim="form.off">
            <el-option label="是" :value="true" />
            <el-option label="否" :value="false" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="setMallFormData = false">取消</el-button>
          <el-button type="primary" @click="updataMall"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import MallSale from "../components/MallSale.vue";
import MallCart from "../components/MallCart.vue";
import api from "../API";
import { onMounted, ref } from "vue";
import { useStore } from "vuex";
const stoer = useStore();
const mallList = ref([]);
const loading = ref(false);
const setMallFormData = ref(false);

const form = ref({});
//初始化
onMounted(async () => {
  loading.value = true;
  await getMallList();
  loading.value = false;
});

//获取商品数据
const getMallList = async () => {
  const res = await api.getMallData();

  mallList.value = res;
  stoer.dispatch("getMallData", res);
};

//修改商品上下架状态
const handleSetOff = async (obj) => {
  loading.value = true;
  await api.setMallOff(obj);
  await getMallList();
  loading.value = false;
};
//修改商品信息
const updataMall = async () => {
  if (rulesForm(form.value)) {
    loading.value = true;
    await api.setMallData(form.value);
    await getMallList();
    setMallFormData.value = false;
    loading.value = false;
  }
};

//点击设置按钮
const setMall = (data) => {
  form.value = { ...data };
  setMallFormData.value = true;
};

//点击图片
const handleImg = () => {
  ElMessage.info("实在不知道图片上传应该怎么去模拟~/(ㄒoㄒ)/~~ 请谅解！");
};

//验证表单
const rulesForm = (formEl) => {
  if (formEl.name.length === 0 || formEl.price.length === 0) {
    ElMessage.error("名称与价格不能为空");
    return false;
  } else {
    if (
      (+formEl.price && +formEl.discount) ||
      (+formEl.price && formEl.discount.length === 0)
    ) {
      return true;
    } else {
      ElMessage.error("请填写正确的价格");
      return false;
    }
  }
};
</script>

<style lang="less" scoped>
.mall-content {
  margin-top: 10px;
  display: flex;
  justify-content: start;
  flex-wrap: wrap;
  & > div {
    width: 23.5%;
    margin-left: 15px;
    margin-top: 10px;
  }
}
</style>
